package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.ShopAddressContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class
 * @time 2020/2/16
 */
public class ShopAddressPresenter extends RxPresenter<ShopAddressContract.View> implements ShopAddressContract.Presenter {

    private DataManager mDataManager;

    private int currentType = Constants.TYPE_ADDRESS;

    @Inject
    public ShopAddressPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(ShopAddressContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == currentType;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "搜索地址") {
                    @Override
                    public void onNext(SendEvent s) {
                        getData();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    @Override
    public void getData() {
        addSubscribe(mDataManager.getPtiaddresslist()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<AddressListResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<AddressListResponBean>> optional) {
                        if (mView != null)
                            mView.showContent(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showContentError();
                    }
                })
        );

    }

    @Override
    public void deleteAddress(int id) {
        addSubscribe(mDataManager.deletePtngoodsList(id)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                            @Override
                            public void onNext(Optional<BaseResponse> optional) {
                                if (mView != null)
                                    mView.deleteSuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if (mView != null)
//                            mView.showContentError();
                            }
                        })
        );
    }
}
